package com.gmail.dengtao.joe.group.protocol.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.gmail.dengtao.joe.group.protocol.IllegalProtocolException;
import com.gmail.dengtao.joe.group.protocol.Protocol;
import com.gmail.dengtao.joe.group.protocol.ProtocolFactory;
import com.gmail.dengtao.joe.group.protocol.ProtocolParser;

public class ProtocolFactoryImpl extends ProtocolFactory {

	private Map<String, ProtocolParser> protocolMap = new ConcurrentHashMap<String, ProtocolParser>();
	
	@Override
	public Protocol parse(int type, byte[] data) {
		String symbol = type + "";
		ProtocolParser parser = protocolMap.get(symbol);
		if (parser != null) {
			return parser.parse(data);
		} else {
			throw new IllegalProtocolException();
		}
	}

	@Override
	public void register(String name, ProtocolParser protocol) {
		protocolMap.put(name, protocol);
	}

	@Override
	public void deregister(String name) {
		protocolMap.remove(name);
	}

}
